package com.lechuang.goods.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.lechuang.goods.dto.GoodsRegionSetSpuDTO;
import com.lechuang.goods.entity.*;
import com.lechuang.goods.manager.GoodsRegionManager;
import com.lechuang.goods.service.*;
import com.lechuang.goods.vo.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsRegionManagerImpl implements GoodsRegionManager {
    @Resource
    private GoodsRegionService goodsRegionService;
    @Resource
    private GoodsRegionSpuService goodsRegionSpuService;
    @Resource
    private GoodsRegionSkuService goodsRegionSkuService;
    @Resource
    private GoodsSpuService goodsSpuService;
    @Resource
    private GoodsSkuService goodsSkuService;
    @Resource
    private GoodsCategoryService goodsCategoryService;

    @Override
    public List<GoodsRegionListVO> list(Integer regionType) {
        List<GoodsRegion> list = goodsRegionService.list(regionType);
        return BeanUtil.copyToList(list, GoodsRegionListVO.class);
    }

    @Override
    public void setSpu(GoodsRegionSetSpuDTO goodsRegionSetSpuDTO) {
        Long id = goodsRegionSetSpuDTO.getId();
        List<GoodsSku> goodsSkuList = goodsSkuService.getBySpuIds(goodsRegionSetSpuDTO.getSpuIdList());
        Map<Long, List<GoodsSku>> skuMap = goodsSkuList.stream().collect(Collectors.groupingBy(GoodsSku::getSpuId));

        List<GoodsRegionSpu> goodsRegionSpuList = new ArrayList<>();
        List<GoodsRegionSku> goodsRegionSkuList = new ArrayList<>();

        goodsRegionSetSpuDTO.getSpuIdList().forEach(spuId -> {
            GoodsRegionSpu goodsRegionSpu = new GoodsRegionSpu()
                    .setRegionId(id)
                    .setSpuId(spuId);
            goodsRegionSpuList.add(goodsRegionSpu);
            if(skuMap.containsKey(spuId)){
                List<GoodsRegionSku> skuList = new ArrayList<>();
                skuMap.get(spuId).forEach(bean -> {
                    GoodsRegionSku sku = new GoodsRegionSku()
                            .setRegionId(id)
                            .setSpuId(spuId)
                            .setSkuId(bean.getId());
                    skuList.add(sku);
                });
                goodsRegionSkuList.addAll(skuList);
            }
        });
        goodsRegionSpuService.saveBatch(goodsRegionSpuList);
        goodsRegionSkuService.saveBatch(goodsRegionSkuList);
    }

    @Override
    public List<GoodsRegionSpuVO> goodsRegionSpu(Long categoryId) {

        List<GoodsRegionSpuVO> goodsRegionSpuVOList = goodsRegionSpuService.getByCategoryId(categoryId);
        Set<Long> spuIdSet = goodsRegionSpuVOList.stream().map(GoodsRegionSpuVO::getSpuId).collect(Collectors.toSet());

        List<GoodsRegionSkuVO> skuList = goodsRegionSkuService.getBySpuIds(spuIdSet);
        Map<Long, List<GoodsRegionSkuVO>> skuMap = skuList.stream().collect(Collectors.groupingBy(GoodsRegionSkuVO::getSpuId));
        goodsRegionSpuVOList.forEach(bean -> {
            Long spuId = bean.getSpuId();
            if(skuMap.containsKey(spuId)){
                bean.setSkuList(skuMap.get(spuId));
            }
        });
        return goodsRegionSpuVOList;
    }

    @Override
    public List<GoodsRegionCategoryVO> goodsRegionCategory(Long id) {
        //查询区域和商品关联关系
        List<GoodsRegionSpu> goodsRegionSpuList = goodsRegionSpuService.getByRegionId(id);
        if(CollUtil.isEmpty(goodsRegionSpuList)){
            return new ArrayList<>();
        }
        //查询spu
        List<Long> spuIdList = goodsRegionSpuList.stream().map(GoodsRegionSpu::getSpuId).toList();
        List<GoodsSpu> spuList = goodsSpuService.getByIds(spuIdList);
        //查询商品上挂的分类
        List<Long> categoryIdList = spuList.stream().map(GoodsSpu::getCategoryId).toList();
        List<GoodsCategory> categoryList = goodsCategoryService.getByIds(categoryIdList);
        //过滤出来所有的二级分类
        List<GoodsCategory> secondCategoryList = categoryList.stream().filter(a -> a.getParentId() != 0).collect(Collectors.toList());
        List<Long> firstCategoryIdList = secondCategoryList.stream().map(GoodsCategory::getParentId).toList();
        List<GoodsCategory> firstCategoryList = goodsCategoryService.getByIds(firstCategoryIdList);
        secondCategoryList.addAll(firstCategoryList);
        //过滤1级分类
        List<GoodsCategory> firstCategory = secondCategoryList.stream().filter(a -> a.getLevel() == 1).toList();
        List<GoodsRegionCategoryVO> voList = new ArrayList<>();
        //遍历1级分类
        firstCategory.forEach(bean -> {
            //获取二级分类
            List<GoodsCategory> childList = secondCategoryList.stream().filter(a -> Objects.equals(a.getParentId(), bean.getId())).toList();
            GoodsRegionCategoryVO vo = BeanUtil.copyProperties(bean, GoodsRegionCategoryVO.class);
            List<GoodsRegionCategoryVO> childVoList = new ArrayList<>();
            childList.forEach(childBean -> {
                GoodsRegionCategoryVO childVo = BeanUtil.copyProperties(childBean, GoodsRegionCategoryVO.class);
                childVoList.add(childVo);
            });
            vo.setChild(childVoList);
            voList.add(vo);
        });
        return voList;
    }
}
